package com.wikimi.check.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wikimi.check.service.CheckService;
import com.wikimi.util.PagingHelper_Check;
import com.wikimi.util.PagingHelper_Check_Time;

@Controller
public class CheckController {

	@Autowired
	private CheckService service;

	@RequestMapping(value = "/location_icur", method = RequestMethod.GET)
	public String getIndexPage(Model model,
			Authentication authentication) {
		
		if(authentication != null) {
			model.addAttribute("id", authentication.getName());
		}
		
		return "location_icur";
	}	

	@RequestMapping(value = "/location_ipath", method = RequestMethod.GET)
	public String getAllPathByPage(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
			Model model,
			Authentication authentication) {
		
		if(authentication != null) {
			model.addAttribute("id", authentication.getName());
		}
		
		PagingHelper_Check helper = new PagingHelper_Check(service.getPathTotalById(authentication.getName()));
		
		model.addAttribute("page", page);
		model.addAttribute("link", helper.getPageLink(page.intValue()));
		model.addAttribute("kids", service.getAllKidById(authentication.getName()));
		model.addAttribute("checks", service.getAllPathByPage(page, authentication.getName()));
		
		return "location_ipath";
	}
	
	@RequestMapping(value = "/location_ipath_time", method = RequestMethod.POST)
	public String getAllPathPageByTime(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
			@RequestParam(value = "cust_i_hp_no", required = false, defaultValue = "") String cust_i_hp_no,
			@RequestParam(value = "startTime", required = false, defaultValue = "") String startTime,
			Model model,
			Authentication authentication) {
		
		if(authentication != null) {
			model.addAttribute("id", authentication.getName());
		}
		
		PagingHelper_Check_Time helper = new PagingHelper_Check_Time(service.getPathTotalByIdTime(authentication.getName(), cust_i_hp_no, startTime));
		
		model.addAttribute("page", page);
		model.addAttribute("link", helper.getPageLink(page.intValue()));
		model.addAttribute("kids", service.getAllKidById(authentication.getName()));
		model.addAttribute("checks", service.getAllPathByTime(page, authentication.getName(), cust_i_hp_no, startTime));
		
		return "location_ipath";
	}
}



